package com.naiterui.ehp.bs.patient.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.constants.CommonConstant;
import com.naiterui.ehp.bs.patient.handler.data.BizConfigEvent;

/**
 * @author gaoliangliang
 * @date 2019-08-23 15:22
 * @since 1.0.0
 */
@Component
public class BizConfigHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(BizConfigHandler.class);

    /**
     * 微信客服消息结果异常处理
     *
     * @param publishMsgEvent
     */
    @Async
    @EventListener(classes = BizConfigEvent.class, condition = "#root.args[0].bizType == " + BizConfigEvent.BIZ_CONFIG_WXMSG_LIMIT)
    public void publishFail(BizConfigEvent bizConfigEvent) {

        String openId = bizConfigEvent.getSource();
        String result = bizConfigEvent.getEventData();
        LOGGER.error("微信客服消息推送异常，openId={},result={}", openId, result);

        RedisUtil.keyOps().delete(CommonConstant.CONSULT_PATIENT_ACTIVE + openId);
    }

    /**
     * 免责协议校验开
     *
     * @param publishMsgEvent
     */
    @Async
    @EventListener(classes = BizConfigEvent.class, condition = "#root.args[0].bizType == " + BizConfigEvent.BIZ_CONFIG_DISCLAIMER_SWITCH_ON)
    public void disclaimerSwitchOn(BizConfigEvent bizConfigEvent) {

        LOGGER.info("免责协议校验开启");
        RedisUtil.valueOps().set(CommonConstant.CONSULT_DISCLAIMER_SWITCH, "ON");
    }

    /**
     * 免责协议校验关
     *
     * @param publishMsgEvent
     */
    @Async
    @EventListener(classes = BizConfigEvent.class, condition = "#root.args[0].bizType == " + BizConfigEvent.BIZ_CONFIG_DISCLAIMER_SWITCH_OFF)
    public void disclaimerSwitchOff(BizConfigEvent bizConfigEvent) {

        LOGGER.info("免责协议校验关闭");
        RedisUtil.keyOps().delete(CommonConstant.CONSULT_DISCLAIMER_SWITCH);
    }

}
